package com.lovo.bbs.bo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.naming.NamingException;

import com.lovo.bbs.dao.UserInfoDao;
import com.lovo.bbs.po.UserInfoPo;
import com.lovo.bbs.vo.UserInfoVo;

/**
 * 用户信息Bo
 * 
 * @author tiancen2001
 * 
 */
public class UserInfoBo {
	public static  final int TYPE_OF_MEMBER=0;//普通会员标志
	public static  final int TYPE_OF_MODERATOR=1;//版主标志
	public static  final int TYPE_OF_ADMIN=2;//超级管理员标志
	
	 //操作类型
	 public static final int OPER_OF_UPDATE=2;
	 public static final int OPER_OF_DELETE=3;
	
/**
 *  返回指定用户名的用户Vo
 * @param userName
 * @param type 用户类型
 * @return
 */
	public  UserInfoVo getUserInfo(String userName,int type)
		 {
		UserInfoVo vo = new UserInfoVo();
		UserInfoPo po=null;
		
		if(userName==null){
			userName="";
		}
		try {
			po = new UserInfoDao().getUserInfo(userName,type);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
        if(po!=null){//找到
        	vo.setUserId(po.getUserId());
    		vo.setUserName(po.getUserName());
    		vo.setEmail(po.getEmail());

    		vo.setMsn(po.getMsn());
    		vo.setQq(po.getQq());
    		vo.setHomePage(po.getHomePage());
    		vo.setBlog(po.getBlog());
    		vo.setBirthday(po.getBirthday());
    		vo.setHome(po.getHome());

    		vo.setScore(po.getScore());

    		vo.setRankName(po.getRankName());
    		vo.setRankImg(po.getRankImg());

    		vo.setHeadName(po.getHeadName());
    		vo.setHeadImg(po.getHeadImg());
    		vo.setSignature(po.getSignature());
    		
    		vo.setCustomHeadImg(po.isCustomHeadImg());//自定义头像标志

    		vo.setTopicNum(po.getTopicNum());// 发表主题数
    		
    		vo.setType(po.getType());
        }else{
        	vo=null;
        }
		return vo;
	}

	/**
	 * 发主题帖奖励
	 * 
	 * @param authorname
	 * @return
	 */
	public  int addOneTopic(String authorname) {
		int added = 0;
		try {
			added = new UserInfoDao().addOneTopic(authorname);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return added;
	}

	/**
	 * 回帖奖励
	 * 
	 * @param postAuthorName
	 */
	public  int addOnePost(String postAuthorName) {
		int added = 0;
		try {
			added = new UserInfoDao().addOnePost(postAuthorName);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return added;
	}

	/**
	 * 修改邮件或密码
	 * 
	 * @param uservo
	 * @param formerpassword
	 * @return
	 */
	public  int chgMailPassword(UserInfoVo uservo, String formerpassword) {
		int changed = 0;
		UserInfoPo po = new UserInfoPo();

		po.setUserName(uservo.getUserName());
		po.setEmail(uservo.getEmail());
		po.setPassword(uservo.getPassword());
		

		try {
			changed = new UserInfoDao().chgMailPassword(po, formerpassword);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return changed;

	}

	/**
	 * 修改联系信息
	 * @param uservo
	 * @return
	 */
	public  int chgContactInfo(UserInfoVo vo) {
		int changed = 0;
		UserInfoPo po = new UserInfoPo();
		po.setUserName(vo.getUserName());
		po.setHomePage(vo.getHomePage());
		po.setMsn(vo.getMsn());
		po.setBlog(vo.getBlog());
		po.setQq(vo.getQq());
		po.setHome(vo.getHome());
		try {
			changed = new UserInfoDao().chgContactInfo(po);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return changed;
	}

	/**
	 * 修改用户头像
	 * @param vo
	 * @param headImg
	 * @return
	 */
	public  int updateUserHead(UserInfoVo vo, String headImg) {
        int changed=0;
        UserInfoPo po = new UserInfoPo();
        po.setUserName(vo.getUserName());
        po.setSignature(vo.getSignature());
        po.setHeadImg(vo.getHeadImg());
        po.setCustomHeadImg(vo.isCustomHeadImg());
        try {
			changed=new UserInfoDao().updateUserHead(po,headImg);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return changed;
		
	}
	
	/**
	 * 返回所有用户
	 */
	public  ArrayList<UserInfoVo> getUsers(int page){
		ArrayList<UserInfoVo> vos = new ArrayList<UserInfoVo>();
		ArrayList<UserInfoPo> pos = null;
		try {
			pos = new UserInfoDao().getUsers(page);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Iterator<UserInfoPo> it = pos.iterator();
		while(it.hasNext()){
			UserInfoPo po = it.next();
			UserInfoVo vo = new UserInfoVo();
			
			vo.setUserId(po.getUserId());
    		vo.setUserName(po.getUserName());
    		vo.setEmail(po.getEmail());

    		vo.setMsn(po.getMsn());
    		vo.setQq(po.getQq());
    		vo.setHomePage(po.getHomePage());
    		vo.setBlog(po.getBlog());
    		vo.setBirthday(po.getBirthday());
    		vo.setHome(po.getHome());

    		vo.setScore(po.getScore());

    		vo.setRankName(po.getRankName());
    		vo.setRankImg(po.getRankImg());

    		vo.setHeadName(po.getHeadName());
    		vo.setHeadImg(po.getHeadImg());
    		vo.setSignature(po.getSignature());
    		
    		vo.setCustomHeadImg(po.isCustomHeadImg());//自定义头像标志

    		vo.setTopicNum(po.getTopicNum());// 发表主题数
    		
    		vo.setType(po.getType());
			
			vos.add(vo);
		}
		return vos;
	}
	
	/**
	 * 计算用户数
	 */
	public  int getUserNum(){
		int userNum=0;
		try {
			userNum = new UserInfoDao().getUserNum();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userNum;
	}
	
	/**
	 * 控制台更新
	 */
	public  int  updateUser(UserInfoVo vo,int operType){
		int updated=0;
		
		UserInfoPo po = new UserInfoPo();
		po.setUserId(vo.getUserId());
		po.setEmail(vo.getEmail());
		po.setScore(vo.getScore());
		po.setTopicNum(vo.getTopicNum());
		
		try {
			updated = new UserInfoDao().updateUser( po,operType);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}

	/**
	 * 删除用户
	 * @param userid
	 */
	public  int delUser(int userid) {
		int del= 0;
		try {
			del = new UserInfoDao().delUser(userid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return del;
	}
}
